﻿namespace Furesoft.Styling.Converters
{
    using System.Text.RegularExpressions;

    internal class TimeConverter : IConverter<Time>
    {
        #region Public Methods and Operators

        public override Time Convert(string s)
        {
            string pattern = @"(?<num>[1-9](?:\d{0,2}(?:,\d{3})+|\d*))(?<time>s|ms|m|h)?";
            Match m = Regex.Match(s, pattern);
            string time = m.Groups["time"].Value;
            string num = m.Groups["num"].Value;

            if (time == "s")
            {
                return new Time(int.Parse(num) * 1000);
            }
            if (time == "m")
            {
                return new Time(int.Parse(num) * 1000 * 60);
            }
            if (time == "h")
            {
                return new Time(int.Parse(num) * 1000 * 60 * 60);
            }
            if (time == "ms")
            {
                return new Time(int.Parse(num));
            }

            return new Time(int.Parse(s));
        }

        public override string Convert(Time s)
        {
            return s.value + "ms";
        }

        #endregion
    }
}